/**
 * Created by Administrator on 2018/10/29 0029.
 */
//登陆controller--------------------------------------------------------------------------------------------------------------------------------------
angular.module('loginapp').controller("loginCtrl", loginCtrl);  //用于账户的管理  账户的添加 删除   列表等操作
loginCtrl.$inject = ["$scope", "$http", "$element", "NgTableParams", "userinfoTalbedateService"];
//上面定义过之后  下面只要位置正确 名字可以和上面不一样
function loginCtrl($scope, $http, $element, NgTableParams, simpleList) {

    $scope.qhgs = FuturesSetting
    var self = this;
    $scope.myVar = false;

    // self.tableParams = new NgTableParams({page: 1, count: 5}, {counts: [5, 10], dataset: simpleList});
    $scope.simplieList = simpleList
    self.checkboxes = {
        checked: false,
        items: {}
    };

    // watch for check all checkbox
    $scope.$watch(function () {
        return self.checkboxes.checked;
    }, function (value) {
        angular.forEach(simpleList, function (item) {
            self.checkboxes.items[item.id] = value;
        });
    });

    // watch for data checkboxes
    $scope.$watch(function () {
        return self.checkboxes.items;
    }, function (values) {
        var checked = 0, unchecked = 0,
            total = simpleList.length;
        angular.forEach(simpleList, function (item) {
            checked += (self.checkboxes.items[item.id]) || 0;
            unchecked += (!self.checkboxes.items[item.id]) || 0;
        });
        if ((unchecked == 0) || (checked == 0)) {
            self.checkboxes.checked = (checked == total);
        }
        // grayed checkbox
        angular.element($element[0].getElementsByClassName("select-all")).prop("indeterminate", (checked != 0 && unchecked != 0));
    }, true);


    //form表单相关
    $scope.buttontitle = "新增";
    $scope.id = 0;
    $scope.UserName = '';
    $scope.apikey = '';
    $scope.secretkey = '';
    $scope.selectqhgs = '';


    function qingkongform() {
        $scope.buttontitle = "新增";
        $scope.UserName = $scope.apikey = $scope.secretkey = $scope.selectqhgs = '';
        $scope.id = 99999;
        $scope.myVar = false;
    }

    $scope.quxiaoedit = function () {
        //取消编辑
        qingkongform()
    }
    $scope.edituser = function (buttontitle) {
        //参数可能是(新增  保存   id)
        //新增的时候新加一个对象    保存的时候保存现有form数据并且到table     为id时候把table上面的数据放到form上面
        if (buttontitle == '新增') {
            //{"id": 12, "name": "231213", "pass": "***", "qhgs":  '大陆期货',"statusClass":"label label-primary","status":"未启动"}
            obj = {};
            obj.id = simpleList.length;
            obj.username = $scope.UserName;
            obj.apikey = $scope.apikey;
            obj.secretkey = $scope.secretkey
            obj.Futures = $scope.selectqhgs;
            obj.positiontype = "zhu"
            obj.url = "www.okex.com"
            obj.statusClass = "label label-primary";
            obj.status = "未启动";
            //后台实现  提交后台   如果成功了执行这里
            simpleList.push(obj);

            $scope.UserName = $scope.apikey = $scope.selectqhgs = $scope.secretkey = '';
            $scope.myVar = false;

        } else if (buttontitle == "保存") {      // 用户点击的是保存按钮  保存对象
            //后台实现  然后下列操作
            obj = simpleList[$scope.id];

            obj.username = $scope.UserName;
            obj.apikey = $scope.apikey;
            obj.secretkey = $scope.secretkey
            obj.Futures = $scope.selectqhgs;
            qingkongform()

        } else {       //用户点击的是table上面的修改按钮  执行的把table数据放到form上 并把按钮初始为保存状态

            obj = simpleList[buttontitle];
            $scope.id = obj.id;
            $scope.UserName = obj.username;
            $scope.apikey = obj.apikey;
            $scope.secretkey = obj.secretkey
            $scope.selectqhgs = obj.Futures;
            $scope.buttontitle = "保存";
            $scope.myVar = true;

        }
    };
    //用户登陆
    $scope.loginuser = function () {

        templist = []
        Object.keys(self.checkboxes.items).forEach(function (key) {
            if (self.checkboxes.items[key] == true) {
                templist.push(simpleList[key].username) //["adong", "chengcheng", "xiangke", "xiangke2"]
            }
        });  //得到所有选择用户名//["adong", "chengcheng", "xiangke", "xiangke2"]

        $http.get("login",
            {
                params: templist
            }).success(function (data) {

            for (i in data.message) {   //['145646']
                for (j in simpleList) {     //[{name:'145646','sdf':sddsf}]
                    if (data.message[i] == simpleList[j].username) {            //设置为登陆状态
                        simpleList[j]['statusClass'] = "label label-success"
                        simpleList[j]['status'] = '已启动'
                    }
                }
            }
        }).error(function () {

        });
    };

};
//登陆controller结束--------------------------------------------------------------------------------------------------------------------------------------
